﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Idte.Core.Infrastructure.Repository
{
    
    public abstract class AggregateBase<TProperty> : IAggregate<TProperty> where TProperty : AggregateProperty, new()
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        public ICollection<TProperty> Properties { get; set; }
        public TProperty this[string propertyName]
        {
            get
            {
                if (Properties.Any(m => m.Name == propertyName))
                    return Properties.Where(m => m.Name == propertyName).FirstOrDefault();
                else
                {
                    TProperty newProperty = new TProperty();
                    newProperty.Name = propertyName;
                    Properties.Add(newProperty);
                    return newProperty;
                }
            }

            set
            {
                Properties.Remove(Properties.Where(m => m.Name == propertyName).FirstOrDefault());
                Properties.Add(value);
            }
        }
    }
}
